//
//  GameObject.m
//  Template
//
//  Created by Hilary Muludiang on 7/1/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "GameObject.h"


@implementation GameObject
@synthesize reactsToScreenBoundaries;
@synthesize screenSize;
@synthesize isActive;
@synthesize gameObjectType;

-(id)init{
	if ((self = [super init])) {
		CCLOG(@"GameObject init");
		screenSize = [CCDirector sharedDirector].winSize;
		isActive = TRUE;
		gameObjectType = kObjectTypeNone;
	}
	return self;
}

-(void)changeState:(CharacterStates)newState{
	CCLOG(@"GameObject->changeState method should be overridden");
}

-(void)updateStateWithDeltaTime:(ccTime)deltaTime andListOfGameObjects:(CCArray *)listofGameObjects{
	CCLOG(@"updateStateWithDeltaTime method should be overrriden");
}

-(CGRect)adjustedBoundingBox{
	CCLOG(@"GameObject adjustedBoundingBox should be overridden");
	return [self boundingBox];
}

-(CCAnimation *)loadPlistForAnimationWithName:(NSString *)animationName andClassName:(NSString *)className{
	CCAnimation *animationToReturn = nil;
	NSString *fullFileName = [NSString stringWithFormat:@"%@.plist",className];
	NSString *plistPath;
	
	//Get path to plist file
	NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
	plistPath = [rootPath stringByAppendingPathComponent:fullFileName];
	if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]){
		plistPath = [[NSBundle mainBundle] pathForResource:className ofType:@"plist"];
	}
	
	//Read in the plist file
	NSDictionary *plistDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
	
	//Get the mini-dictionary for this animation
	NSDictionary *animationSettings = [plistDictionary objectForKey:animationName];
	if (animationSettings == nil){
		CCLOG(@"Could not locate AnimationWithName:%@",animationName);
		return nil;
	}
	
	//Get the delay value for the animation
	float animationDelay = [[animationSettings objectForKey:@"delay"] floatValue];
	animationToReturn = [CCAnimation animation];
	[animationToReturn setDelay:animationDelay];
	
	//Add the frame to the animation
	NSString *animationFramePrefix = [animationSettings objectForKey:@"filenamePrefix"];
	NSString *animationFrames = [animationSettings objectForKey:@"animationFrames"];
	NSArray *animationFrameNumbers = [animationFrames componentsSeparatedByString:@","];
	
	for (NSString *frameNumber in animationFrameNumbers){
		NSString *frameName = [NSString stringWithFormat:@"%@%@.png",animationFramePrefix,frameNumber];
		[animationToReturn addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName]];
	}
	
	return animationToReturn;
}
@end
